package edu.gatech.startrader.gui;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;


import edu.gatech.startrader.Game;
import edu.gatech.startrader.SaveState;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

import javax.swing.JLabel;
import javax.swing.ImageIcon;

public class GameStartScreen extends JDialog {

	private final JPanel contentPanel = new JPanel();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) 
	{
		try 
		{
			GameStartScreen dialog = new GameStartScreen();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} 
		catch (Exception e) 
		{
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public GameStartScreen() {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
        }
		setBounds(100, 100, 640, 405);
		getContentPane().setLayout(null);
		contentPanel.setBounds(0, 0, 850, 570);
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel);
		contentPanel.setLayout(null);
		
		JButton btnStartNewGame = new JButton("Start New Game");
		btnStartNewGame.setBounds(193, 108, 254, 70);
		contentPanel.add(btnStartNewGame);
		
		JButton btnLoadSavedGame = new JButton("Load Saved Game");
		btnLoadSavedGame.setBounds(193, 185, 254, 70);
		contentPanel.add(btnLoadSavedGame);
		
		JButton btnExitGame = new JButton("Exit Game");
		btnExitGame.setBounds(193, 261, 254, 70);
		contentPanel.add(btnExitGame);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setBounds(0, 0, 640, 400);
		contentPanel.add(lblNewLabel);
		lblNewLabel.setIcon(new ImageIcon(GameStartScreen.class.getResource("/edu/gatech/startrader/gui/TitleScreen.jpeg")));
		btnLoadSavedGame.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{
				JFileChooser fc = new JFileChooser();
				int returnVal = fc.showSaveDialog(null);
				if(returnVal == JFileChooser.APPROVE_OPTION)
				{
					File file = fc.getSelectedFile();
					Game game = SaveState.load(file).getSaveInfo();
					new MainGameScreen(game).setVisible(true);
					setVisible(false);
					dispose();
				}
			}
		});
		btnStartNewGame.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				setVisible(false);
				PlayerConfigScreen.configurePlayer(null);
				dispose();
			}
		});
	}

}
